import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http'
import { map, Observable } from 'rxjs';
import { environment } from 'src/environments/environment';
import {  Category } from './types';
import { AlbumArgs, AlbumsInfo, CategoryInfo } from 'src/app/pages/albums/albums.component';
import * as qs from 'qs';

@Injectable({
  providedIn: 'root'
})
export class AlbumService {
  readonly prefix = '/xmly/';
  constructor(private http: HttpClient) { }

  /**
   * 一级分类列表
   *
   * @param {number} [categoryId=3] 默认选择3
   * @return {*}  {Observable<Category[]>}
   * @memberof AlbumService
   */
  categories(categoryId: number = 3): Observable<Category[]> {
    const params = new HttpParams().set('categoryId', categoryId.toString());
    return this.http
      .get(`${environment.baseUrl}${this.prefix}breadcrumb`, { params })
      .pipe(map((res: any) => res.data.categories));
  }


  /**
   * 二三级分类列表
   *
   * @param {(Pick<AlbumArgs, 'category' | 'subcategory'>)} args
   * @return {*}  {Observable<CategoryInfo>}
   * @memberof AlbumService
   */
  detailCategoryPageInfo(args: Pick<AlbumArgs, 'category' | 'subcategory'>): Observable<CategoryInfo> {
    const params = new HttpParams( { fromString: qs.stringify(args)});
    return this.http
      .get(`${environment.baseUrl}${this.prefix}categories`, { params })
      //.pipe(map((res: Base<CategoryInfo>) => res.data));
      .pipe(map((res: any) => res.data));
  }


  /**
   * 专辑列表
   *
   * @param {AlbumArgs} args
   * @return {*}  {Observable<AlbumsInfo>}
   * @memberof AlbumService
   */
  albums(args: any): Observable<AlbumsInfo> {
    const params = new HttpParams({ fromString: qs.stringify(args) });
    return this.http.get(`${environment.baseUrl}${this.prefix}albums`, { params })
      .pipe(map((res: any) => res.data));
  }
}
